package com.spring.campus_express_delivery.service.serviceImpl;

import com.spring.campus_express_delivery.dao.SchoolAddressDao;
import com.spring.campus_express_delivery.entity.Orders;
import com.spring.campus_express_delivery.entity.PageBean;
import com.spring.campus_express_delivery.entity.SchoolAddress;
import com.spring.campus_express_delivery.service.SchoolAddressService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class SchoolAddressServiceImpl implements SchoolAddressService {
    @Resource
    SchoolAddressDao schoolAddressDao;
    //订单列表
    @Override
    public PageBean<SchoolAddress> selectServiceAddressByPage(int size, int page, String sort, String asc) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<SchoolAddress> pageBean = new PageBean<SchoolAddress>();
        //封装当前页数
        pageBean.setCurrPage(page);
        //封装页数大小
        pageBean.setPageSize(size);
        //封装排序字段
        pageBean.setPageSort(sort);
        //封装排序规则
        pageBean.setPageAsc(asc);

        //封装总记录数
        int totalCount = schoolAddressDao.selectCountByServiceAddress();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num =Math.ceil(tc/size);//向上取整
        pageBean.setTotalPage(num.intValue());


        map.put("PageStart",(page-1)*size);
        map.put("PageSize", pageBean.getPageSize());
        map.put("PageSort", sort);
        map.put("PageAsc", asc);

        //封装每页显示的数据
        List<SchoolAddress> lists = schoolAddressDao.selectServiceAddressByPage(map);
        pageBean.setLists(lists);
        return pageBean;
    }

    //统计数目
    public int  selectCountByServiceAddress(){
        return schoolAddressDao.selectCountByServiceAddress();
    }

    @Override
    public void insertServiceAddress(SchoolAddress schoolAddress) {
        schoolAddressDao.insertServiceAddress(schoolAddress);
    }

    @Override
    public SchoolAddress selectByServiceAddressId(int id) {
        return schoolAddressDao.selectByServiceAddressId(id);
    }

    @Override
    public void updateSchoolAddress(SchoolAddress schoolAddress) {
        schoolAddressDao.updateSchoolAddress(schoolAddress);
    }

    @Override
    public void deleteSchoolAddressById(Integer id) {
        schoolAddressDao.deleteSchoolAddressById(id);
    }

    @Override
    public SchoolAddress selectByServiceAddress(String serviceAddress) {
        return schoolAddressDao.selectByServiceAddress(serviceAddress);
    }

}
